package com.jp.knowledge.logic;

import android.content.Context;

import com.google.gson.JsonObject;
import com.jiayen.dialog.LoadingDialog;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.ResultSubscriber;

/**
 * Created by jiayen on 16/11/16.
 * <p>
 * 搜索logic  全局搜索类
 */

public class SearchLogic implements ResultSubscriber.OnResultListener {
    private Context mContext;
    private LoadingDialog loadingDialog;

    private SearchCallBack  searchCallBack;
    public SearchLogic(Context mContext) {
        this.mContext = mContext;
        loadingDialog =new LoadingDialog(mContext);
    }


    public void setSearchCallBack(SearchCallBack searchCallBack) {
        this.searchCallBack = searchCallBack;
    }
    /**
     * 执行搜索
     */
    public void search(String keyWord, int type, int subType, int pageNo, String action, String id, int aciontType, int level) {
        JsonObject param = new JsonObject();
        if(keyWord != null && keyWord.trim().length() > 0)
            param.addProperty("keyword",  keyWord);
        if(action != null && action.trim().length() > 0)
            param.addProperty("recommendAction", action);
        if(id != null && id.trim().length() > 0)
            param.addProperty("id", id);
        param.addProperty("pageNo", pageNo);
//        if (type == -1){  // 报告
//            param.addProperty("type", 14);
//            param.addProperty("subType", "1,2,3,4,5,7,9,14,15");
//        } else if(type == 6) {
//            param.addProperty("type", 14);
//            param.addProperty("subType", "1");
//        } else if(type == 14){
//            param.addProperty("type", 14);
//            param.addProperty("subType", "3");
//        } else{
//            param.addProperty("type", type);
//        }
        if(type == -1){
            type = 14;
            param.addProperty("subType", "1,2,3,4,5,7,9,14,15");
        } else if(type == 6 || type == 14) {
            type = 14;
            param.addProperty("subType", subType+"");
        }
        param.addProperty("type", type);
        if(type == 15)
            param.addProperty("action", aciontType+"");
        if(type==13){//知识的另外搜索
            param.addProperty("action", aciontType);
            param.addProperty("level", level);
            HttpHelper.getInstance(mContext).knowlageSearch(param, 1, this);
        }else {
            HttpHelper.getInstance(mContext).commonSearch(param, 1, this);
        }
    }



    @Override
    public void onStart(int requestType) {
    }

    @Override
    public void onCompleted(int requestType) {
        loadingDialog.cancel();
        if(searchCallBack!=null)
            searchCallBack.compelete();
    }

    @Override
    public void onError(int requestType) {
        loadingDialog.cancel();
        if(searchCallBack!=null)
            searchCallBack.compelete();
    }

    @Override
    public void onNext(IModel data, int requestType) {
        int code = data.getErrcode();
        if(code==0){
            if(searchCallBack!=null)
                searchCallBack.done(data);
        }else{
            ToasUtil.toast(mContext,"搜索失败，请稍后再试!");
        }
    }
    public interface  SearchCallBack{
        void done(IModel data);
        void compelete();
    }
}
